package com.karl.mytask_2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.bean.MyUser;
import com.karl.mytask_2.bean.Task;
import com.karl.mytask_2.tools.BaseActivity;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Karl on 2016/5/21.
 */
public class PublishActivity extends BaseActivity implements View.OnClickListener {
    private TextView mToolBarTitle;
    private ImageButton mBack;

    private EditText mPublishTitle,mPublishContent,mPublishAddress,mPublishPrice;
    private String mTitle,mContent,mAddress,mPrice;
    private Button mPublishBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.publish);
        initView();
        initEvent();

    }

    private void initView(){
        mToolBarTitle = (TextView) findViewById(R.id.tool_bar2_title);
        mBack = (ImageButton) findViewById(R.id.tool_bar2_back);

        mPublishTitle = (EditText) findViewById(R.id.publish_title);
        mPublishContent = (EditText) findViewById(R.id.publish_content);
        mPublishAddress = (EditText) findViewById(R.id.publish_address);
        mPublishPrice = (EditText) findViewById(R.id.publish_price);
        mPublishBtn = (Button) findViewById(R.id.publish_btn);

    }
    private void initEvent(){
        mToolBarTitle.setText("发布任务");

        //设置监听事件
        mBack.setOnClickListener(this);
        mPublishBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tool_bar2_back:
                PublishActivity.this.finish();
                break;
            case R.id.publish_btn:
                Toast.makeText(PublishActivity.this, "正在提交发布，请稍等", Toast.LENGTH_SHORT).show();
                toPublish();
                break;
        }
    }

    private void toPublish(){
        getEditTextData();
        if (mTitle.equals("")|| mContent.equals("")||mAddress.equals("")|| mPrice.equals("")){
            Toast.makeText(PublishActivity.this, "所有项不能为空", Toast.LENGTH_SHORT).show();
        }else {
            MyUser user = BmobUser.getCurrentUser(PublishActivity.this, MyUser.class);
            Task  myTask = new Task();
            myTask.setTitle(mTitle);
            myTask.setContent(mContent);
            myTask.setAddress(mAddress);
            myTask.setPrice(mPrice);
            myTask.setPublisher(user);
            myTask.setStatus("0");
            myTask.save(getApplicationContext(), new SaveListener() {
                @Override
                public void onSuccess() {
                    Toast.makeText(PublishActivity.this, "发布成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    setResult(1,intent);
                    PublishActivity.this.finish();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(PublishActivity.this, "发布失败！原因："+s, Toast.LENGTH_SHORT).show();
                }
            });

        }
    }
    //获取输入数据
    private void getEditTextData(){
        mTitle = mPublishTitle.getText().toString();
        mContent = mPublishContent.getText().toString();
        mAddress = mPublishAddress.getText().toString();
        mPrice = mPublishPrice.getText().toString();
    }
}
